package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempDealOrderDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempDealOrder;
import com.sc.nft.entity.vo.SecondOrderExportListVO;
import com.sc.nft.enums.ExportRecordsTypeEnum;
import com.sc.nft.enums.ExportTypeEnum;
import com.sc.nft.enums.ExportTypeStatusEnum;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.TempDealOrderMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.SecondOrderService;
import com.sc.nft.service.TempDealOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description: 交易列表
 * @date 2024-07-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempDealOrderServiceImpl implements TempDealOrderService {

    private final SecondOrderService secondOrderService;
    private final ExportRecordsDao exportRecordsDao;

    private final TempDealOrderMapper tempDealOrderMapper;

    private final TempDealOrderDao TempDealOrderDao;


    @Override
    public Boolean saveTempDealOrder(Date startTime, Date endTime, Integer marketType, List<Long> collectionIdList) {
        Long exportRecordsId = getExportRecordsId(marketType);

        List<SecondOrderExportListVO> secondOrderExportListVOS = secondOrderService.collectionExport(collectionIdList, marketType, startTime, endTime);
        List<TempDealOrder> tempDealOrderList = BeanUtil.copyToList(secondOrderExportListVOS, TempDealOrder.class);
        if (CollectionUtils.isEmpty(tempDealOrderList)) {
            throw new GlobalRunTimeException("导出数据为空");
        }
        try {
            tempDealOrderList.forEach(t -> t.setExportRecordsId(exportRecordsId));
            tempDealOrderMapper.saveBatch(tempDealOrderList);
        } catch (Exception e) {
            //导出失败
            log.error("交易列表导出失败:", e);

            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }

        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }


    private static Long getExportRecordsId(Integer marketType) {

        StringBuilder exportName = new StringBuilder();
        int exportType = 0;

        if (MarketTypeEnum.BRAND_INCUBATION == MarketTypeEnum.getMarketTypeEnum(marketType)) {
            exportName.append(ExportTypeEnum.TEMP_BRAND_INCUBATION.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_BRAND_INCUBATION.getCode();

        } else {
            exportName.append(ExportTypeEnum.TEMP_FREE_MARKET.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_FREE_MARKET.getCode();
        }
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempDealOrder> findTempDealOrderList(Long exportRecordsId) {
        return TempDealOrderDao.getTempDealOrderList(exportRecordsId);
    }

}
